package config

import (
	"git.ycyz.org/nothingness/fecho/gopkg.in/yaml.v2"
	"io/ioutil"
	"os"
	"utils"
)

var MyConfig *Config

type Config struct {
	RootUrl    string `yaml:"root_url" `
	SearchCode string `yaml:"search_code" `
	ReqUrl     string `yaml:"req_url"`
	AppId      int    `yaml:"app_id"`
	Sign       string `yaml:"sign"`
	Account    string `yaml:"account"`
	Password   string `yaml:"password"`
}

//从数据解析配置
func parseConfigData(data []byte) (*Config, error) {
	var cfg Config
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		return nil, err
	}
	MyConfig = &cfg
	return &cfg, nil
}

//从文件解析配置
func ParseConfigFile(fileName string) (*Config, error) {
	path, _ := os.Getwd()
	filePath := path + fileName
	if utils.Exist(filePath) {
		os.Remove(filePath)
	}
	utils.CopyFile(filePath,"/Users/pk001/custom_project/convertHttp/src/config"+fileName)
	data, err := ioutil.ReadFile(filePath)
	if err != nil {
		return nil, err
	}
	return parseConfigData(data)
}
